
#cutDownTo - parameter to equalise against (that is to cut data down to such a size, so that each value of qualiser occurs with the same frequency)
#selectors -    list of key-value pairs denoting rows to extract from the table 
#               create it as crit<-list(); crit["breast"]="left"; crit["img-type"]="cc";

craftData <- function(dataFrame, cutDownTo=NULL, selectors=NULL){

    resultDataFrame<-data.frame();
    crit = vector(mode="logical", length=(dim(x=dataFrame)[1]));    
    crit[] = TRUE;
    if(is.null(selectors)){
#        print("selectors is null")
    }else{
#        print("selectors ain't null")
        for(i in 1:length(selectors)){
            crit=crit&dataFrame[names(selectors[i])]==selectors[i];       
        }        
    }
    dataFrame<-dataFrame[crit,]

    if(is.null(cutDownTo)){
        #print("cutDownTo is null");    
    }else{
        #print("cutDownTo ain't null");
        supports=vector();
        values=unique(dataFrame[cutDownTo]);
        masks=matrix(ncol=dim(values)[1],nrow=dim(dataFrame)[1]);
        
        for(i in 1:dim(values)[1]){
            #print(dataFrame[cutDownTo])            
            logVec=(dataFrame[cutDownTo]==as.character(values[i,1]));
            masks[,i]<-logVec;

            supports[i]=(sum(masks[,i]));
#            print(supports[i]);        
        }
        minSup = min(supports);
#        print(minSup);
        for(i in 1:dim(values)[1]){
            if(i==1){
                resultDataFrame=dataFrame[masks[,i],][ sample( 1:supports[i], minSup, replace=FALSE), ];
                #print("Some dimensions...")
                #print(dim(resultDataFrame))
            }else{            
                resultDataFrame=rbind(resultDataFrame,dataFrame[masks[,i],][ sample( 1:supports[i], minSup, replace=FALSE), ]);
            }
        }
        dataFrame<-resultDataFrame;  
    }
    dataFrame;
}
